﻿///*
// * SearchCoordinateSystem
// * 
// * initial implementation : 20.11.2008
// *
// * Copyright (c) Adam Sida, asida@centrum.cz.
// * All rights reserved.
// * 
// * for license see Petra.Gis.License.txt
// */
//using System;
//using System.IO;
//using System.Windows.Markup;
//using Petra.Gis.ReferenceSystemServices.Transformations;


//namespace Petra.Gis.ReferenceSystemServices
//{

//  public static class SearchCoordinateSystem
//  {

//    public static CoordinateSystemList CoordinateSystems;



//        /// <summary>
//    /// static constructor loads predefined transform cache from xaml file...
//    /// </summary>
//    static SearchCoordinateSystem()
//    {
//      CoordinateSystemCache _cstCache;

//      // load transform definition xml file

//      string path;

////#if (DEBUG)
////      //path = "CoordinateSystemCache.xcs";
////      path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
////      path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString(), 
////             @"..\..\CoordinateSystemCache.xcs");
////#else
//      // create some object from this assembly, to get path from reflection
//      // _cstCache must be located with this dll
//      //System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
//      //System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);

//      Axis s = new Axis();
//      System.Reflection.Assembly asm = System.Reflection.Assembly.GetAssembly(s.GetType());
//      System.IO.FileInfo f = new System.IO.FileInfo(asm.Location);

//      path = f.Directory + "\\CoordinateSystemCache.xcs";
////#endif
      
//      //System.Xml.XmlDocument data = new System.Xml.XmlDocument();
//      //data.Load(path);
//      FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);

//      _cstCache = (CoordinateSystemCache)XamlReader.Load(fs);
//      CoordinateSystems = _cstCache.CoordinateSystems;

//      fs.Close();


//    }




//    public static CoordinateSystem ByAuthorityAndCode(string authority, string code, string storageUnitName)
//    {

//      CoordinateSystem result = null;

//      // search first in coordinate systems...
//      foreach (CoordinateSystem cs in CoordinateSystems)
//      {
//        if (cs.Metadata.Authority == authority && cs.Metadata.AuthorityCode.ToString() == code)
//          return resolveNamedCsStorageUnits(cs, storageUnitName);

//      }

//      // ok, not found
//      // try search in transforms collection...
//      foreach (SRSTransform t in SearchTransform.Transforms)
//      {
//        if (t.Source.Metadata.Authority == authority && t.Source.Metadata.AuthorityCode.ToString() == code)
//          //result =  t.Source;
//          return resolveNamedCsStorageUnits(t.Source, storageUnitName);

//        else if(t.Target.Metadata.Authority == authority && t.Target.Metadata.AuthorityCode.ToString() == code)
//          //result = t.Target;
//          return resolveNamedCsStorageUnits(t.Target, storageUnitName);
//      }

//      // if coordinate system found
//      // and units are specified as input parameter
//      // try change coordinate system units
//      //if (result != null && storageUnitName != "")
//      //{
//      //  // try resolve storage units..
//      //  return resolveNamedCsStorageUnits(result, storageUnitName);
//      //}

//      return null;
//    }
    




//    private static CoordinateSystem resolveNamedCsStorageUnits(CoordinateSystem cs, string inputStorageUnitName)
//    {
//      // if coordinate system found
//      // and units are specified as input parameter
//      if (inputStorageUnitName == "")
//        return cs;

//      // try change coordinate system units
//      // try resolve storage units..
//      // search for unit by given name
//      SpatialUnit su = SpatialUnit.Parse(inputStorageUnitName);

//      // if unit not found, return null
//      if (su == null)
//        return null;

//      // if some unit found, check for sure
//      // if type is same (angular or distance)
//      if (cs.Unit.GetType() != su.GetType())
//        return null;
//      //throw new ArgumentException("specified coordinate system in:" + csInfo +
//      //                            " has " + su.GetType().ToString() + " storage units  and " +
//      //                            " found coordinate system has " + result.Unit.GetType().ToString());

//      // if given unit is different from cs one
//      // clone cs and replace
//      if (!cs.Unit.EqualParams(su))
//      {
//        CoordinateSystem result = cs.Clone();
//        result.Unit = su;
//        return result;
//      }
//      else
//        // or leave and return cs untouched
//        return cs;

//    }


//  }
//}
